package com.proton.carepatchtemp.login.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.base.BaseFragment;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;


public class Guide3Fragment extends BaseFragment {
    private View rootView;
    private Profile mProfile;
    private SimpleDraweeView mSelectUserImage;
    /**
     * ---
     */
    private TextView mTvName;
    /**
     * -
     */
    private TextView mTvSex;
    /**
     * --岁
     */
    private TextView mTvBirthday;

    public static Guide3Fragment newInstance() {
        Guide3Fragment f = new Guide3Fragment();
        Bundle args = new Bundle();
        f.setArguments(args);
        return f;
    }


    @Override
    public void dealEvent(MainEvent event) {
        int id = event.getId();
        switch (id) {
            case Codes.NET_SIGN_GUIDE:
                mProfile = (Profile) event.getObj();
                if (mProfile == null) return;
                mSelectUserImage.setImageURI(mProfile.getAvatar());
                mTvName.setText(mProfile.getRealname());
                if (1 == mProfile.getGender()) {
                    mTvSex.setText(getString(R.string.string_male));
                } else {
                    mTvSex.setText(getString(R.string.string_female));
                }
                mTvBirthday.setText(mProfile.getBirthday());
                break;

            default:
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_guide3, null);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。 
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        initView(rootView);
        return rootView;
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initView(View rootView) {
        mSelectUserImage = rootView.findViewById(R.id.select_user_image);
        mTvName = rootView.findViewById(R.id.tv_name);
        mTvSex = rootView.findViewById(R.id.tv_sex);
        mTvBirthday = rootView.findViewById(R.id.tv_birthday);
    }
}
